package com.topone.permission.sercurity.service;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;
import com.topone.permission.model.UserSeesion;
import com.topone.permission.result.Result;
import com.topone.permission.utils.ControllerUtils;

/**
 * 登录成功
 * @author XiaZhentao
 * @date 2018年12月1日
 */
@Component
public class MyAuthenticationSuccessHandler implements AuthenticationSuccessHandler{

	@Override
	public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
			Authentication authentication) throws IOException, ServletException {
		UserSeesion user = ControllerUtils.getUser();
		Collection<GrantedAuthority> authorities = user.getAuthorities();
		
		List<String> perssions=new ArrayList<>();
		for (GrantedAuthority grantedAuthority : authorities) {
			perssions.add(grantedAuthority.getAuthority());
		}
		Result<List<String>> result = Result.success(perssions);
		ControllerUtils.SuccessfulRepose(request, response, result);
	}

}
